import { Message } from 'element-ui'
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router =  new Router({
  routes: [
    {
      path: '/',
      redirect: '/switch'
    },
    {
      path: '/login',
      name: 'login',
      component: require('@/pages/login').default,
      meta:{
        auth:false
      }
    },
    {
      path: '/switch',
      name: 'switch',
      component: require('@/pages/switch').default,
      meta:{
        auth:true,
        keepAlive: true
      }
    },
    {
      path: '/idconfig',
      name: 'idconfig',
      component: require('@/pages/idconfig').default,
      meta:{
        auth:true,
        keepAlive: true
      }
    },
    {
      path: '*',
      redirect: '/'
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(item => item.meta.auth)) {
    let token = localStorage.getItem("token");
    if(token!==undefined&&token!==""&&token!==null){
      next()
    } else {
      // if(from.fullPath=== '/login' && to.fullPath==='/switch'){
        
      // }
      if(from.fullPath=== '/' && to.fullPath==='/switch'){

      } else {
        Message({
          type:'error',
          message:'请先登录',
          showClose:true
        })
      }
      next({
          path: '/login',
      })
    }
  }
  next();
})

export default router;